<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Model\Order;
use App\Model\Refund;
use App\Tool\Json;

class HomeController extends AuthCheckController
{
    /**
     * @Auth(type='skip_auth')
     * Create by Peter
     * 2019/08/28 10:54:58
     * Email:904801074@qq.com
     * @return false|string
     */
    function home(){


        return Json::code(1,'success',resolve('adminInfo'));
    }


    /**
     * @Auth(type='skip_auth')
     * Create by Peter
     * 2020/07/13 16:43:50
     * Email:904801074@qq.com
     * @return false|string
     */
    function orderInfo(){


        $data=[
          'OrderWithToday'=>$this->getOrderWithToday(),
          'OrderWithYesterday'=>$this->getOrderWithYesterday(),
          'getUserWithToday'=>$this->getUserWithToday(),
          'getUserWithYesterday'=>$this->getUserWithYesterday(),
          'getMoneyWithToday'=>$this->getMoneyWithToday(),
          'getMoneyWithYesterday'=>$this->getMoneyWithYesterday(),
          'getTotalMoney'=>$this->getTotalMoney(),
          'getRefundWithToday'=>$this->getRefundWithToday(),
          'getRefundWithYesterday'=>$this->getRefundWithYesterday(),
        ];


        return Json::code(1,'success',$data);

    }


    /**
     * 获取今日付款订单
     * Create by Peter
     * 2020/07/13 16:34:00
     * Email:904801074@qq.com
     */
    function getOrderWithToday(){


        return Order::where('pay_time',">",strtotime(date('Y-m-d')." 00:00:00"))
            ->where('pay_time','<',strtotime(date('Y-m-d')." 23:59:59"))
            ->count();


    }

    /**
     * 获取昨天付款订单
     * Create by Peter
     * 2020/07/13 17:00:29
     * Email:904801074@qq.com
     * @return int
     */
    function getOrderWithYesterday(){

        return Order::where('pay_time','>',strtotime(date("Y-m-d 00:00:00",strtotime("-1 day"))))
            ->where('pay_time','<',strtotime(date("Y-m-d 23:59:59",strtotime("-1 day"))))
            ->count();

    }

    /**
     * 获取今日付款人数
     * Create by Peter
     * 2020/07/13 16:34:00
     * Email:904801074@qq.com
     */
    function getUserWithToday(){


        return Order::where('pay_time',">",strtotime(date('Y-m-d')." 00:00:00"))
            ->where('pay_time','<',strtotime(date('Y-m-d')." 23:59:59"))
            ->groupBy('user_id')
            ->count('user_id');


    }


    /**
     * 获取昨天付款人数
     * Create by Peter
     * 2020/07/13 17:00:29
     * Email:904801074@qq.com
     * @return int
     */
    function getUserWithYesterday(){

        return Order::where('pay_time','>',strtotime(date("Y-m-d 00:00:00",strtotime("-1 day"))))
            ->where('pay_time','<',strtotime(date("Y-m-d 23:59:59",strtotime("-1 day"))))
            ->groupBy('user_id')
            ->count('user_id');

    }

    /**
     * 获取今日订单金额
     * Create by Peter
     * 2020/07/13 17:08:06
     * Email:904801074@qq.com
     * @return int|mixed
     */
    function getMoneyWithToday(){

        return Order::where('pay_time',">",strtotime(date('Y-m-d')." 00:00:00"))
            ->where('pay_time','<',strtotime(date('Y-m-d')." 23:59:59"))
            ->sum('real_money');


    }

    /**
     * 获取昨日订单金额
     * Create by Peter
     * 2020/07/13 17:09:20
     * Email:904801074@qq.com
     * @return int|mixed
     */
    function getMoneyWithYesterday(){

        return Order::where('pay_time','>',strtotime(date("Y-m-d 00:00:00",strtotime("-1 day"))))
            ->where('pay_time','<',strtotime(date("Y-m-d 23:59:59",strtotime("-1 day"))))
            ->sum('real_money');

    }

    /**
     * 获取订单总金额
     * Create by Peter
     * 2020/07/13 17:10:34
     * Email:904801074@qq.com
     */
    function getTotalMoney(){


        return Order::where('status',11)->orWhere('status',30)->sum('real_money');

    }


    /**
     * 获取今日退款数量
     * Create by Peter
     * 2020/07/13 17:23:04
     * Email:904801074@qq.com
     * @return int
     */
    function getRefundWithToday(){


        return Refund::whereDate('created_at',date('Y-m-d'))->count();


    }

    /**
     * 获取昨天退款数量
     * Create by Peter
     * 2020/07/13 17:26:35
     * Email:904801074@qq.com
     * @return int
     */
    function getRefundWithYesterday(){


        return Refund::whereDate('created_at',date("Y-m-d",strtotime("-1 day")))->count();

    }


    function getOrderList(){

        $list=Order::with('user')
            ->with('address')
            ->with('address_copy')
            ->with('store_copy')
            ->with('order_transaction')->with('order_goods')->orderBy('id','desc')->limit(10)->get();


        return Json::code(1,'success',$list);

    }

}
